package com.cityfood.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cityfood.common.pojo.CityFoodResult;
import com.cityfood.common.utils.JsonUtils;
import com.cityfood.common.utils.MyStringUtils;
import com.cityfood.pojo.CfOrder;
import com.cityfood.pojo.CfOrderItem;
import com.cityfood.pojo.CfProduct;
import com.cityfood.pojo.CfProductSpecs;
import com.cityfood.service.AdminUserService;
import com.cityfood.service.OrderItemService;
import com.cityfood.service.OrderService;
import com.cityfood.service.ProductService;
import com.cityfood.service.ProductSpecsService;
import com.cityfood.service.UserService;

@Controller
public class DeliveryManController {
	
	@Autowired
	private AdminUserService adminUserService;
	
	@Autowired
	private OrderService orderService;
	
	@Autowired
	private OrderItemService itemService;
	
	//注入商品操作接口
	@Autowired
	private ProductService productService;

	@Autowired
    private ProductSpecsService specsService;
	
	
	@RequestMapping(value="/deliverymans/index",method=RequestMethod.GET)
	@ResponseBody
	public CityFoodResult getDeliveryman(@RequestParam Integer uId){
		
		return null;
		
	}
	
	@RequestMapping(value="/deliverymans/tasks",method=RequestMethod.GET)
	@ResponseBody
	public CityFoodResult taskOrderItem(@RequestParam(defaultValue="0") Integer deliverStatus,@RequestParam Integer uid){
		
		CityFoodResult cityFoodResult = new CityFoodResult();
		List<CfOrderItem> itemList = itemService.selectOrderItemByAdminUserId(uid, deliverStatus);
		List<Map<String,Object>> ls = new ArrayList<>();
		for (CfOrderItem item : itemList) {
			Map<String,Object> mp = new LinkedHashMap<>();
			CfOrder order = orderService.getOrder(item.getOrderId());
			
			mp.put("orderId", order.getId());
			mp.put("orderItemId", item.getId());
			//读取订单表商品规格json数据，转为map
			Map<String,Integer> productSpecsMap = (Map<String,Integer>)JsonUtils.jsonToMap(order.getProductSpecsIds());
			//遍历查询到订单信息里面的所以规格信息
			for (String specsId : productSpecsMap.keySet()) {
				//根据规格id查询到规格信息
				CfProductSpecs specs = specsService.getProductSpecsBySpecsId(Integer.parseInt(specsId));
				//根据规格找到商品
				CfProduct cfProduct = productService.getProductByProductId(specs.getProductId());
				
				mp.put("productId", cfProduct.getId());
				mp.put("productName", cfProduct.getName());
				mp.put("productPic", cfProduct.getMainImage());
				mp.put("price", specs.getPrice());
				mp.put("num", productSpecsMap.get(specsId));
				mp.put("attrs", MyStringUtils.getSpecsStr(specs.getProductSpecs()));
				break;
			}
			mp.put("totalPrice", order.getTotalMoney());
			mp.put("reciverName", order.getConsignee());
			mp.put("reciverPhone", order.getPhone());
			mp.put("address", order.getAddress());
			ls.add(mp);
		}
		if (ls.size()>0) {
			Map<String,Object> ms = new LinkedHashMap<>();
			ms.put("total", ls.size());
			ms.put("tasks", ls);
			cityFoodResult.setCode(201);
			cityFoodResult.setMsg("查询配送员任务列表成功");
			cityFoodResult.setData(ms);
			return cityFoodResult;
		}
		cityFoodResult.setCode(201);
		cityFoodResult.setMsg("查询配送员任务列表失败");
		cityFoodResult.setData("");
		return cityFoodResult;
		
	}
	
	@RequestMapping(value="/deliverymans/dotask",method=RequestMethod.PUT)
	@ResponseBody
	public CityFoodResult dotaskOrderItem(@RequestParam Integer uid,@RequestParam Integer orderItemId){
		
		CityFoodResult cityFoodResult = new CityFoodResult();
		
		CfOrderItem orderItem=new CfOrderItem();
		orderItem.setId(orderItemId);
		orderItem.setOrderStatus(2);
		orderItem.setDeliveryStatus((byte)2);
		orderItem.setDeliveryFinishTime(new Date());
		
		int i = itemService.updateOrderItem(orderItem);
		if (i>0) {
			CfOrderItem item = itemService.selectOrderItemById(orderItemId);
			CfOrder order=new CfOrder();
			order.setId(item.getOrderId());
			order.setTradeStatus((byte)4);
			int j = orderService.updateOrderStatus(order);
			if (j<=0) {
				cityFoodResult.setCode(201);
				cityFoodResult.setMsg("修改订单收货状态失败");
				cityFoodResult.setData(false);
				return cityFoodResult;
			}
			
			cityFoodResult.setCode(200);
			cityFoodResult.setMsg("送达确认成功");
			cityFoodResult.setData(true);
			return cityFoodResult;
		}
		cityFoodResult.setCode(201);
		cityFoodResult.setMsg("送达确认失败");
		cityFoodResult.setData(false);
		return cityFoodResult;
		
	}

}
